import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/user'

// 导入路由组件
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Login from '@/views/Login/index.vue'
import AdminInfo from '@/views/AdminCenter/AdminInfo.vue'
import AdminPassword from '@/views/AdminCenter/AdminPassword.vue'
import SceneryList from '@/views/TravelManage/SceneryList.vue'
import HotelList from '@/views/TravelManage/HotelList.vue'
import RestaurantList from '@/views/TravelManage/RestaurantList.vue'
import SceneryPost from '@/views/PostManage/SceneryPost.vue'
import HotelPost from '@/views/PostManage/HotelPost.vue'
import RestaurantPost from '@/views/PostManage/RestaurantPost.vue'
import UserManage from '@/views/UserManage/index.vue'
import OrderManage from '@/views/OrderManage/index.vue'
import EvaluateManage from '@/views/EvaluateManage/index.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: Login
    }, //登录页
    {
      path: '/',
      component: Layout,
      children: [
        { path: '', component: Home }, //Home页
        {
          path: 'travel',
          children: [
            { path: 'scenery', component: SceneryList }, //景点列表页
            { path: 'hotel', component: HotelList }, //酒店列表页
            { path: 'restaurant', component: RestaurantList } //餐厅列表页
          ]
        }, //旅游管理
        {
          path: 'post',
          children: [
            { path: 'scenery', component: SceneryPost }, //景点攻略页
            { path: 'hotel', component: HotelPost }, //酒店攻略页
            { path: 'restaurant', component: RestaurantPost } //餐厅攻略页
          ]
        }, //攻略管理
        {
          path: '/order',
          component: OrderManage
        }, //订单管理
        {
          path: '/evaluate',
          component: EvaluateManage
        }, //评价管理
        {
          path: '/user',
          component: UserManage
        }, //用户管理
        {
          path: 'admin',
          children: [
            { path: 'info', component: AdminInfo }, //基本资料页
            { path: 'password', component: AdminPassword } //重置密码页
          ]
        } //管理员管理
      ]
    } //布局架子
  ]
})

// 路由导航守卫，没有登录时访问拦截
router.beforeEach((to) => {
  const UserStore = useUserStore()
  if (!UserStore.UserList.token && to.path !== '/login') {
    return '/login' //重定向到登录页面
  }
})

export default router
